﻿<%@ WebHandler Language="C#" Class="home" %>

using System;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using common;
using common.web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing.Imaging;

public class home : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.AddHeader("Access-Control-Allow-Origin", "*");
        string _t = context.Request["t"];
        context.Response.Write(getData(_t));
    }

    protected string getData(string t)
    {
        switch (t)
        {
            case "GetDeptList":
                return GetDeptList();               //部门列表
            case "GetFileTypeByDeptId":
                return GetFileTypeByDeptId();       //文件分类
            case "GetLatestUpload":
                return GetLatestUpload();           //最新上传
            case "GetHotDownload":
                return GetHotDownload();            //热门下载
            case "Search":
                return Search();                    //查询
        }
        return "缺少参数!";
    }

    /// <summary>
    /// 部门列表
    /// </summary>
    protected string GetDeptList()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string sql = "select id, cMc, fSjml, fPx, fJb, cLj, tDate, fUid, fDptid, fIsOk  From [t_Department] with(nolock)";
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.ID = dr["id"].ToString();
                inv.Mc = dr["cMc"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 文件分类
    /// </summary>
    protected string GetFileTypeByDeptId()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _DeptId = func.safeStr(Req("DeptId"));
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(string.IsNullOrEmpty(_DeptId) || func.chkRegex(_DeptId, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string sql = "select * from t_Filecatalogue where fDptid = " + _DeptId + "and fIsOk = 1";
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.ID = dr["id"].ToString();
                inv.Mc = dr["cMlmc"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 最新上传
    /// </summary>
    protected string GetLatestUpload()
    {
        string strWhere = "";
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _DeptId = func.safeStr(Req("DeptId"));
        string _TypeId = func.safeStr(Req("TypeId"));
        if(!string.IsNullOrEmpty(_DeptId) &&func.chkRegex(_DeptId, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "DeptId参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(!string.IsNullOrEmpty(_TypeId) && func.chkRegex(_TypeId, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "TypeId参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if (!string.IsNullOrEmpty(_DeptId)) strWhere = strWhere + " and fDptid = " + _DeptId;
        if (!string.IsNullOrEmpty(_TypeId)) strWhere = strWhere + " and fFileclid = " + _TypeId;        
        string sql = "select Top 14 id,cFilename,fFiletype,tDate,cPic,cDescribe From [t_Filecollection] with(nolock) Where fIschecked = 1 And fIsonsale = 1 " + strWhere + " Order By tDate Desc";
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 热门下载
    /// </summary>
    protected string GetHotDownload()
    {
        string strWhere = "";
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _DeptId = func.safeStr(Req("DeptId"));
        string _TypeId = func.safeStr(Req("TypeId"));
        if(!string.IsNullOrEmpty(_DeptId) && func.chkRegex(_DeptId, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "DeptId参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(!string.IsNullOrEmpty(_TypeId) && func.chkRegex(_TypeId, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "TypeId参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if (!string.IsNullOrEmpty(_DeptId)) strWhere = strWhere + " and fDptid = " + _DeptId;
        if (!string.IsNullOrEmpty(_TypeId)) strWhere = strWhere + " and fFileclid = " + _TypeId;        
        string sql = "select Top 14 id,cFilename,fFiletype,tDate,cPic,cDescribe From [t_Filecollection] with(nolock) Where fIschecked = 1 And fIsonsale = 0 " + strWhere + " Order By fDownloads Desc";
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 查询
    /// </summary>
    protected string Search()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string _keyword = func.safeStr(Req("keyword"));
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "id,cFilename,fFiletype,tDate,cPic,cDescribe","[t_Filecollection] with(nolock) where cFilename like '%" + _keyword + "%'", "order by id desc", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("rsct", rsct);
        jo.Add("pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    protected string getUid()
    {
        string _uid = getID(HttpUtility.UrlDecode(func.NoNull(Req("uid")).Replace("+", "%2b")));
        if (_uid == "0")
            _uid = pg.usid();
        return _uid;
    }

    /// <summary>
    /// 解密id
    /// </summary>
    protected string getID(string OpenID)
    {
        int id = 0;
        if (!string.IsNullOrEmpty(OpenID) && int.TryParse(new KeyPwd().Decrypto(OpenID), out id))
            return id.ToString();
        else
            return "0";
    }

    protected string Req(string s)
    {
        return HttpContext.Current.Request[s];
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}